# Maintainer: Frederik Carlier <frederik.carlier@keysight.com>

_realname=libobjc2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
if [[ ${MSYSTEM} != CLANG* ]]; then
  conflicts=("${MINGW_PACKAGE_PREFIX}-gcc-objc")
fi
pkgver=2.2.1
pkgrel=3
pkgdesc="Objective-C runtime library intended for use with Clang. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/gnustep/libobjc2"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-robin-map"
             'git')
source=("https://github.com/gnustep/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        # Windows: Support building with strict compatibility, backport of https://github.com/gnustep/libobjc2/commit/2466c57c59d827b0ff199aaa6748739da99f8bbb.patch
        "0001-Windows-Support-building-with-strict-compatibility.patch"
        # Fix selector forwarding on x64 Windows - https://github.com/gnustep/libobjc2/pull/313
        "https://github.com/gnustep/libobjc2/commit/47d02a8898777c681d263259b0e70d296813f5ff.patch")
sha256sums=('768ea8c5bd0999a29b5d15781125494f986456c1dc5c51d370fb31852cd31ea1'
            '7ef5b80e2b33317cac280ed24b3fdef746f17af056f70ce267af646c776a03ec'
            'e78cc419a4300739f6e874ba5eb480e2ec5c216be4da51897537d0f213bea8f0')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/0001-Windows-Support-building-with-strict-compatibility.patch
  patch -p1 -i ${srcdir}/47d02a8898777c681d263259b0e70d296813f5ff.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  case ${MSYSTEM} in
    MINGW*|UCRT*)
      export LDFLAGS="-fuse-ld=lld -lstdc++ -lgcc_s"
      ;;
    CLANG*)
      export LDFLAGS="-lc++"
      ;;
  esac

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -DCMAKE_C_COMPILER="clang" \
      -DCMAKE_CXX_COMPILER="clang++" \
      -DSTRICT_APPLE_COMPATIBILITY=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DTESTS=OFF \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake -DTESTS=ON ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake --build .

  declare -a extra_flags
  if [[ ${MSYSTEM} == CLANG* ]]; then
    extra_flags="-E UnexpectedException*"
  fi

  "${MINGW_PREFIX}"/bin/ctest.exe $extra_flags
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
